package com.pfc.others;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
/*  * 
 * guarda lecturas con esta estructura:
 * nombre_lugar.txt
 * 
 * SSID_1 ,LEVEL_1 ,FREC_1 ;SSID_2 ,LEVEL_2 ,FREC_2 ;SSID_3 ,LEVEL_3 ,FREC_3 ...  (lectura 1)
 * SSID_1 ,LEVEL_1 ,FREC_1 ;SSID_2 ,LEVEL_2 ,FREC_2; SSID_3 ,LEVEL_3, FREC_3 ...  (lectura 2)
 * SSID_1 ,LEVEL_1 ,FREC_1 ;SSID_2 ,LEVEL_2 ,FREC_2 ;SSID_3 ,LEVEL_3 ,FREC_3 ...  (lectura 3)
 * ......
 */


public class FileManagement {

	private String directory;
	private String completeName;		// directory/place_PFC.txt
	private String place;
	
		
	
	public FileManagement() {
		super();
		this.directory = "C:/Users/nieves/Documents/MATLAB";
		this.completeName = "";
		this.place = "";
	}

	
	public FileManagement(String cn) {
		this.completeName = cn;
	}
	
	public FileManagement(String directory, String place) {
		super();
		this.directory = directory;
		this.completeName = ""+ directory +"/" + place + "_PFC.txt";
		this.place = place;
	}

	
	public Lectura lineToElements(String linea){
		Lectura lista = new Lectura();
		linea = linea.replaceAll(" ", "");
		String[] s = linea.split(";");
		for (int i=0 ; i<s.length; i++){
			lista.add(stringToElement(s[i]));
		}
		return lista;
	}
	
	
	public Element stringToElement(String linea){
		Element e = new Element();		
		String[] s = linea.split(",");
		e.setBssid(s[0]);
		e.setSsid(s[1]);
		e.setLevel(Integer.parseInt(s[2]));
		e.setFrec(Integer.parseInt(s[3]));
		return e;
	}
	
		
	public ConjuntoLecturas readFile(){
		  File archivo = null;
	      FileReader fr = null;
	      BufferedReader br = null;
	      Lectura lectura = new Lectura();
	      ConjuntoLecturas listaLecturas = new ConjuntoLecturas(this.place);
	 
	      try {
	         archivo = new File (this.completeName);
	         fr = new FileReader (archivo);
	         br = new BufferedReader(fr);
	         String linea="";
	         while((linea = br.readLine())!=null && !linea.equals("\n")){
	           lectura = lineToElements(linea);
	           listaLecturas.add(lectura);
	         }
	      }
	      catch(Exception ex){
	         ex.printStackTrace();
	      }finally{
	         try{                    
	            if( null != fr ){   
	               fr.close(); 
	               br.close();
	            }
	            return listaLecturas;
	         }catch (Exception e2){ 
	            e2.printStackTrace();
	            
	         }
	      }return listaLecturas;
	}

	
	public void writeIntoFile(ConjuntoLecturas listaLecturas){
		FileWriter fichero = null;
        PrintWriter pw = null;
        try{
            fichero = new FileWriter(this.completeName, true);
            pw = new PrintWriter(fichero);
            for (int i=0 ; i<listaLecturas.size(); i++){
            	//System.out.println("escribiendo:  "+ i);
            	Lectura lectura = (Lectura)listaLecturas.get(i);
                pw.println(lectura.toString());
           
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
	           if (null != fichero){
	              fichero.close();
	           	  pw.close();
	           	}
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
	}
	
	
	public ArrayList<String> getFileList(){
		File dir = new File (this.directory);
		ArrayList<String> lista = new ArrayList<String>();
		String[] ficheros = dir.list();
		if (ficheros == null)
			System.out.println("No hay ficheros en el directorio especificado");
		else { 
			 for (int x=0 ; x<ficheros.length ; x++)
			    if (ficheros[x].endsWith("_PFC.txt")){  
			    	lista.add((ficheros[x].split("_PFC.txt"))[0]);
			    }
		}
		return lista;
	}
}
